//
//  uiDoubleClickChecker.hpp
//  smu_test1
//
//  Created by jiayi on 2016/10/2.
//
//

#ifndef uiDoubleClickChecker_hpp
#define uiDoubleClickChecker_hpp

class uiDoubleClickChecker
{
public:
    uiDoubleClickChecker() : _flag(0), _last_end_early(0), _last_end(0){}
    virtual ~uiDoubleClickChecker(){}
    
    void setTime() {
        std::chrono::time_point<std::chrono::system_clock> p1 = std::chrono::system_clock::now();
        ( _flag == 0 ? _last_end_early : _last_end ) =  std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
        _flag = 1 - _flag;
    }
    
    bool isDoubleClick() {
        double delta = std::abs( _last_end - _last_end_early );
        bool d = ( delta > 0 && delta < 500 );
        if( d ){ // clear
            _last_end = 0;
            _flag = 0;
        }
        return d;
    }
    
protected:
    int _flag;
    double _last_end_early;
    double _last_end;
};

#endif /* uiDoubleClickChecker_hpp */
